package com.otdemo.dao;

import com.mysql.cj.util.StringUtils;
import com.otdemo.entity.RestUser;
import com.otdemo.util.CacheUtil;
import com.otdemo.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class RestUserDao {

    public void insert(RestUser user) {
        try {
            Connection connection = new DBUtils().getConnection();
            String sql = "insert into stu_user\n" +
                    "(uid, name, gender, role, image, mobile, email, username, password, deleted, createDate, updateDate,operator) \n" +
                    "values (?, ?, ?, ?, ?, ?, ?, ?, ?,0, now(), now(), ?)";
            String uid = UUID.randomUUID().toString();
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, uid);
            statement.setString(2, user.getName());
            statement.setString(3, user.getGender());
            // 0-管理员，1-用户
            statement.setString(4, "1");
            statement.setString(5, user.getImage());
            statement.setString(6, user.getMobile());
            statement.setString(7, user.getEmail());
            statement.setString(8, user.getUsername());
            statement.setString(9, user.getPassword());
            statement.setString(10, uid);
            statement.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void update(RestUser user) {
        try {
            Connection connection = new DBUtils().getConnection();
            String sql = "update stu_user set \n" +
                    "name       = ?,\n" +
                    "gender     = ?,\n" +
                    "image      = ?,\n" +
                    "mobile     = ?,\n" +
                    "email      = ?,\n" +
                    "username   = ?,\n" +
                    "password   = ?,\n" +
                    "deleted    = ?,\n" +
                    "updateDate = now(),\n" +
                    "operator   = ?\n" +
                    "where uid = ?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, user.getName());
            statement.setString(2, user.getGender());
            statement.setString(3, user.getImage());
            statement.setString(4, user.getMobile());
            statement.setString(5, user.getEmail());
            statement.setString(6, user.getUsername());
            statement.setString(7, user.getPassword());
            statement.setBoolean(8, user.getDeleted());
            statement.setString(9, user.getOperator());
            statement.setString(10, user.getUid());
            statement.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public RestUser findByUid(String queryUid) {
        try {
            Connection connection = new DBUtils().getConnection();
            String sql = "select id, uid, name, gender, role, image, mobile, email, username, password, deleted, createDate, updateDate,operator " +
                    "from stu_user where uid = ? and deleted = 0 ";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, queryUid);
            ResultSet rs = statement.executeQuery();
            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                Integer id = rs.getInt("id");
                String uid = rs.getString("uid");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                String role = rs.getString("role");
                String image = rs.getString("image");
                String mobile = rs.getString("mobile");
                String email = rs.getString("email");
                String username = rs.getString("username");
                String password = rs.getString("password");
                Boolean deleted = rs.getBoolean("deleted");
                Date createDate = rs.getDate("createDate");
                Date updateDate = rs.getDate("updateDate");
                String operator = rs.getString("operator");
                RestUser user = new RestUser();
                user.setId(id);
                user.setUid(uid);
                user.setName(name);
                user.setGender(gender);
                user.setRole(role);
                user.setImage(image);
                user.setMobile(mobile);
                user.setEmail(email);
                user.setUsername(username);
                user.setPassword(password);
                user.setDeleted(deleted);
                user.setCreateDate(createDate);
                user.setUpdateDate(updateDate);
                user.setOperator(operator);
                return user;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public List<RestUser> list() {
        List<RestUser> userList = new ArrayList<>();
        String name1 = CacheUtil.getString(CacheUtil.USER_SEARCH_NAME);
        ResultSet rs;
        try {
            if(StringUtils.isNullOrEmpty(name1)){
                Connection connection = new DBUtils().getConnection();
                String sql = "select id, uid, name, gender, role, image, mobile, email, username, password, deleted, createDate, updateDate,operator " +
                        "from stu_user where deleted = 0 ";
                PreparedStatement statement = connection.prepareStatement(sql);
                rs = statement.executeQuery();
            }else {
                Connection connection = new DBUtils().getConnection();
                String sql = "select id, uid, name, gender, role, image, mobile, email, username, password, deleted, createDate, updateDate,operator " +
                        "from stu_user where deleted = 0 and name like ?";
                PreparedStatement statement = connection.prepareStatement(sql);
                statement.setString(1, "%" + name1 + "%");
                rs = statement.executeQuery();
            }
            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                Integer id = rs.getInt("id");
                String uid = rs.getString("uid");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                String role = rs.getString("role");
                String image = rs.getString("image");
                String mobile = rs.getString("mobile");
                String email = rs.getString("email");
                String username = rs.getString("username");
                String password = rs.getString("password");
                Boolean deleted = rs.getBoolean("deleted");
                Date createDate = rs.getDate("createDate");
                Date updateDate = rs.getDate("updateDate");
                String operator = rs.getString("operator");
                RestUser user = new RestUser();
                user.setId(id);
                user.setUid(uid);
                user.setName(name);
                user.setGender(gender);
                user.setRole(role);
                user.setImage(image);
                user.setMobile(mobile);
                user.setEmail(email);
                user.setUsername(username);
                user.setPassword(password);
                user.setDeleted(deleted);
                user.setCreateDate(createDate);
                user.setUpdateDate(updateDate);
                user.setOperator(operator);
                userList.add(user);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return userList;
    }

    public RestUser currentUser() {
        try {
            Connection connection = new DBUtils().getConnection();
            Statement statement = connection.createStatement();
            String sql = "select id, uid, name, gender, role, image, mobile, email, username, password, deleted, createDate, updateDate,operator " +
                    "from stu_user";
            ResultSet rs = statement.executeQuery(sql);
            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                Integer id = rs.getInt("id");
                String uid = rs.getString("uid");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                String role = rs.getString("role");
                String image = rs.getString("image");
                String mobile = rs.getString("mobile");
                String email = rs.getString("email");
                String username = rs.getString("username");
                String password = rs.getString("password");
                Boolean deleted = rs.getBoolean("deleted");
                Date createDate = rs.getDate("createDate");
                Date updateDate = rs.getDate("updateDate");
                String operator = rs.getString("operator");
                RestUser user = new RestUser();
                user.setId(id);
                user.setUid(uid);
                user.setName(name);
                user.setGender(gender);
                user.setRole(role);
                user.setImage(image);
                user.setMobile(mobile);
                user.setEmail(email);
                user.setUsername(username);
                user.setPassword(password);
                user.setDeleted(deleted);
                user.setCreateDate(createDate);
                user.setUpdateDate(updateDate);
                user.setOperator(operator);
                return user;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

    public RestUser findByUserName(String queryName) {
        try {
            Connection connection = new DBUtils().getConnection();
            String sql = "select id, uid, name, gender, role, image, mobile, email, username, password, deleted, createDate, updateDate,operator " +
                    "from stu_user where username = ?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, queryName);
            ResultSet rs = statement.executeQuery();
            // 展开结果集数据库
            while (rs.next()) {
                // 通过字段检索
                Integer id = rs.getInt("id");
                String uid = rs.getString("uid");
                String name = rs.getString("name");
                String gender = rs.getString("gender");
                String role = rs.getString("role");
                String image = rs.getString("image");
                String mobile = rs.getString("mobile");
                String email = rs.getString("email");
                String username = rs.getString("username");
                String password = rs.getString("password");
                Boolean deleted = rs.getBoolean("deleted");
                Date createDate = rs.getDate("createDate");
                Date updateDate = rs.getDate("updateDate");
                String operator = rs.getString("operator");
                RestUser user = new RestUser();
                user.setId(id);
                user.setUid(uid);
                user.setName(name);
                user.setGender(gender);
                user.setRole(role);
                user.setImage(image);
                user.setMobile(mobile);
                user.setEmail(email);
                user.setUsername(username);
                user.setPassword(password);
                user.setDeleted(deleted);
                user.setCreateDate(createDate);
                user.setUpdateDate(updateDate);
                user.setOperator(operator);
                return user;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
